<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar 
      :focus="true" 
      :radius="100" 
      focus 
      v-model="searchValue"
      cancelButton="none" 
      clearButton="auto"
      @input="input">
      </uni-search-bar>
    </view>
    <!-- 搜索建议 -->
    <view class="search-list">
      <view class="search-item" v-for="item in searchResult" :key="item.goods_id" @click="goToDetail(item.goods_id)">
        <view class="item-goods-name">{{item.goods_name}}</view>
        <uni-icons type="forward" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-show="searchValue.trim() === '' && historys.length !== 0">
      <view class="history-title">
        <text>历史搜索</text>
        <uni-icons type="trash" size="20" @click="deleteAllHistory"></uni-icons>
      </view>
      <view class="history-list">
        <view class="history-item" v-for="(history, index) in historys" :key="index" @click="historySearch(history)">
          {{history}}
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        searchValue: '',
        timer: null,
        // 搜索建议列表
        searchResult: [],
        // 搜索历史
        historyList: []
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('searchHistoryList') || '[]')
    },
    computed: {
      // 历史记录调整展示顺序
      historys() {
        return [...this.historyList].reverse();
      }
    },
    methods: {
      input(e) {
        clearTimeout(this.timer)
        // 防抖
        this.timer = setTimeout(async () => {
          this.searchValue = e.trim();
          this.getSearchList();
        }, 500)
      },
      // 获取搜索建议
      async getSearchList() {
        if (this.searchValue.trim() === '') {
          this.searchResult = [];
          return;
        }
        const result = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.searchValue})
        if (result.data.meta.status !== 200) return uni.$showMsg();
        this.searchResult = result.data.message;
      },
      // 跳转至详情页
      goToDetail(id) {
        // 添加搜索历史记录并去重
        const set = new Set(this.historyList);
        // 保证每次输入的在最前面
        set.delete(this.searchValue);
        set.add(this.searchValue);
        this.historyList = [...set];
        // 保存到本地——持久化
        uni.setStorageSync('searchHistoryList', JSON.stringify(this.historyList));
        // 跳转
        uni.navigateTo({
          url: "/subpkg/goods_detail/goods_detail?goods_id=" + id
        })
      },
      // 删除所有历史记录
      deleteAllHistory() {
        this.historyList = [];
        uni.removeStorageSync('searchHistoryList');
      },
      // 点击历史记录进行搜索
      historySearch(history) {
        console.log(history);
        this.searchValue = history
      }
    }
  }
</script>

<style lang="scss">
  // 搜索框
  .search-box {
    background-color: #C00000;
    position: sticky;
    top: 0;
    z-index: 999;
  }
  // 搜索建议列表
  .search-list {
    .search-item {
      display: flex;
      justify-content: space-between;
      height: 40px;
      border-bottom: 1px solid #efefef;
      line-height: 40px;
      padding: 0 10px;
      .item-goods-name {
        font-size: 12px;
        // 文字不允许换行
        white-space: nowrap;
        overflow: hidden;
        // 文本溢出用...代替
        text-overflow: ellipsis;
      }
    }
  }
  // 搜索历史记录
  .history-box {
    .history-title {
      display: flex;
      justify-content: space-between;
      margin-top: 10px;
      padding: 0 10px;
    }
    .history-list {
      display: flex;
      .history-item {
        background-color: #efefef;
        border-radius: 15px;
        height: 30px;
        padding: 0 15px;
        margin: 10px;
        font-size: 13px;
        line-height: 30px;
        border: 1px solid #efefef;
      }
    }
  }
  
</style>
